package data;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

public class SubForum {
	private Integer _id;
	private String _topic;
	private HashMap<Integer,Moderator> _moderatorCollection;
	private HashMap<Integer,ThreadMessage> _messages;
	private Forum _Forum;
	private Integer _messageIndex;
	private Vector<Member> _bannedUsers;
	
	public SubForum(Integer id, String topic, Forum Forum, Moderator moderator) {
		super();
		_id = id;
		_topic = topic;
		_Forum = Forum;
		_messages= new HashMap<Integer, ThreadMessage>();
		_moderatorCollection= new HashMap<Integer, Moderator>();
		_moderatorCollection.put(moderator.get_id(), moderator);
		_messageIndex=1;
		_bannedUsers=new Vector<Member>();
	}
	
	public SubForum(Integer id, String topic, Forum Forum, HashMap<Integer, Moderator> moderators) {
		super();
		_id = id;
		_topic = topic;
		_Forum = Forum;
		_messages= new HashMap<Integer, ThreadMessage>();
		_moderatorCollection= moderators;
		_messageIndex=1;
		_bannedUsers=new Vector<Member>();
	}
	
	public ThreadMessage getThreadMessage(Integer Index){
		return this._messages.get(Index);
	}

	public Integer get_id() {
		return _id;
	}
	public String get_topic() {
		return _topic;
	}
	public HashMap<Integer, Moderator> get_moderatorCollection() {
		return _moderatorCollection;
	}
	public Map<Integer, ThreadMessage> get_messages() {
		return _messages;
	}
	public Forum get_Forum() {
		return _Forum;
	}
	public void set_topic(String topic) {
		_topic = topic;
	}
	
	public synchronized Integer get_messageIndex() {
		Integer ans = new Integer(_messageIndex);
		_messageIndex++;
		return ans;
	}
	
	public void addThreadMsg(String title, String content, Member composer ,Date date){
		Integer msgIndex = this.get_messageIndex();
		ThreadMessage tThreadmsg = new ThreadMessage(msgIndex, title, content, composer, date, get_id());
		this._messages.put(msgIndex, tThreadmsg);
	}
	
	public boolean addPostMsg(String title, String content, Member composer ,Date date,Integer threadMsgIndex,Integer responseMsgIndex)
	{
		Integer msgIndex = this.get_messageIndex();
		ThreadMessage threadMsg = this._messages.get(threadMsgIndex);
		if (threadMsg == null)
			return false;
		ForumMessage responseTo;
		if (threadMsgIndex.equals(responseMsgIndex))
			responseTo = threadMsg;
		else
			responseTo = threadMsg.getPostedMsg(responseMsgIndex);
		if (responseTo == null)
			return false;
		PostMessage addedPostMsg = new PostMessage(msgIndex, title, content, composer, date, _id, threadMsg, responseTo);
		threadMsg.addNewResponse(addedPostMsg);
		return true;
	}
	
	// NEED TO FIX HERE!! - _moderatorCollection posses Moderator objects and not Integer.
	private boolean isPremited(Integer modifierID,Integer composerId){  
		if (modifierID.equals(composerId))
			return true;
		if(_moderatorCollection.containsKey(modifierID)){ 
			// need to add condition which check the period of time moderator is in his position. 
			//more than a month is true.
				Moderator moderator=_moderatorCollection.get(modifierID);
				Date startModeratorDate = moderator.get_startDate();
				if (checkMoreThenMonth(startModeratorDate))
					return true;
		}
		return false;
	}
	
	public boolean checkMoreThenMonth(Date moderatorStartDate){
		Date currentDate = new Date(System.currentTimeMillis());
		long totalTime = (long)currentDate.getTime() - (long)moderatorStartDate.getTime();
		long day =(long) 1000*60*60*24;
		return (totalTime/day) >=30;
	}
	
	public boolean updateThreadMessage(Integer userId,Integer msgId, String title,String content){
		if (this._messages.containsKey(msgId)){
			return false;
		}
		ThreadMessage message = this._messages.get(msgId);
		if (!isPremited(userId, message.get_composer().get_id())){
			return false;
		}
		if(title!=null)
			message.set_title(title);
		if(content!=null)
			message.set_content(content);
		return true;
	}
	
	public boolean updatePostMessage(Integer userId,Integer threadMsgId,Integer postMsgId, String title,String content){
		if (!this._messages.containsKey(threadMsgId)){
			return false;
		}
		ThreadMessage threadMsg = this._messages.get(threadMsgId);
		if (!threadMsg.containPost(postMsgId)){
			return false;
		}
		PostMessage message = threadMsg.getPostedMsg(postMsgId);
		if (!isPremited(userId, message.get_composer().get_id())){
			return false;
		}
		if(title!=null)
			message.set_title(title);
		if(content!=null)
			message.set_content(content);
		return true;
	}
	
	public boolean deletePostMessage(Integer userId,Integer threadMsgId,Integer postMsgId){
		if (!this._messages.containsKey(threadMsgId)){
			return false;
		}
		ThreadMessage threadMsg = this._messages.get(threadMsgId);
		if (!threadMsg.containPost(postMsgId)){
			return false;
		}
		PostMessage message = threadMsg.getPostedMsg(postMsgId);
		if (!isPremited(userId, message.get_composer().get_id())){
			return false;
		}
		threadMsg.deletePostMsg(postMsgId);
		return true;
	}
	
	public boolean deleteThreadMessage(Integer userId,Integer threadMsgId){
		if (!this._messages.containsKey(threadMsgId)){
			return false;
		}
		ThreadMessage threadMsg = this._messages.get(threadMsgId);
		if (!isPremited(userId, threadMsg.get_composer().get_id())){
			return false;
		}
		this._messages.remove(threadMsgId);
		return true;
	}
	
	public boolean banMember(Integer moderatorId,Member user){
		if (!isPremited(moderatorId, null))
			return false;
		if(!_bannedUsers.contains(user))
			_bannedUsers.add(user);
		return true;
	}
}
